package com.cowain.system.service.impl;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cowain.base.core.domain.entity.SysUserRoleEntity;
import com.cowain.base.core.domain.vo.SysUserVo;
import com.cowain.system.mapper.SysUserRoleMapper;
import com.cowain.system.service.SysUserRoleService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author songxz
 * @date 2022/11/15
 */
@Service
public class SysUserRoleServiceImpl extends ServiceImpl<SysUserRoleMapper, SysUserRoleEntity> implements SysUserRoleService {

    /**
     * 插入用户和角色关系
     *
     * @param user
     * @return
     */
    @Override
    public boolean insertUserRole(SysUserVo user) {
        boolean flag = true;
        Long[] roleIds = user.getRoleIds();
        if(ArrayUtil.isNotEmpty(roleIds)){
            List<SysUserRoleEntity> insertList = new ArrayList<>();
            Arrays.stream(roleIds).forEach(s->{
                SysUserRoleEntity entity=SysUserRoleEntity
                        .builder()
                        .userId(user.getUserId())
                        .roleId(s)
                        .build();
                insertList.add(entity);
            });
            flag = this.saveBatch(insertList);
        }
        return flag;
    }
}
